package com.xbongbong.saas.enums.business;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.ScreenTypeEnum;
import com.xbongbong.saas.enums.SetTypeEnum;
import com.xbongbong.saas.enums.ShowTypeEnum;
import com.xbongbong.saas.enums.base.RefundBaseEnum;
import lombok.Getter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 退货单
 *
 * @author wujian
 * @version v1.0
 * @date 2020/12/22 14:05
 * @since v1.0
 */
@Getter
public enum ReturnEnum {
    // 退货单模板设置界面展示字段
    // 基本信息
    BASE_INFO(RefundBaseEnum.BASE_INFO.getAttr(), "enum.return.attrName.base_info", "baseInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 10000, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    RETURN_NO(RefundBaseEnum.REFUND_NO.getAttr(), "enum.return.attrName.return_no", "returnNo", FieldTypeEnum.SERIALNO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9990, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    LINK_DISTRIBUTOR(RefundBaseEnum.CUSTOMER_ID.getAttr(), "enum.return.attrName.distributor_user_id", "linkDistributor", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9980, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ORDER_ID(RefundBaseEnum.CONTRACT_ID.getAttr(), "enum.return.attrName.order_id", "orderId", FieldTypeEnum.LINK_BUSINESS_SINGLE.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9970, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    RETURN_NAME(RefundBaseEnum.REFUND_NAME.getAttr(), "enum.return.attrName.refund_name", "refundName", FieldTypeEnum.TEXT.getType(), SetTypeEnum.DISABLE_NONE.getCode(), 0, 0, ShowTypeEnum.ALL.getCode(), 9960, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    SIGN_USER(RefundBaseEnum.SIGN_USER.getAttr(), "enum.return.attrName.sign_user", "signUser", FieldTypeEnum.USER.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9950, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    SIGN_DATE(RefundBaseEnum.SIGN_DATE.getAttr(), "enum.return.attrName.sign_date", "signDate", FieldTypeEnum.DATETIME.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9940, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    OWNER_ID(RefundBaseEnum.OWNER_ID.getAttr(), "enum.return.attrName.owner_id", "ownerId", FieldTypeEnum.OWNERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9930, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    COUSERID(RefundBaseEnum.COUSERID.getAttr(), "enum.return.attrName.couserid", "coUserId", FieldTypeEnum.COUSERID.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9920, 1, 0, ScreenTypeEnum.SCREEN_SEARCH.getType()),
    // 扩展信息
    RETURN_INFO(RefundBaseEnum.RETURN_INFO.getAttr(), "enum.return.attrName.return_info", "returnInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9910, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PRODUCTS(RefundBaseEnum.PRODUCTS.getAttr(), "enum.return.attrName.products", "products", FieldTypeEnum.SELECT_PRODUCT.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9900, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    RETURN_TIPS(RefundBaseEnum.RETURN_TIPS.getAttr(), "enum.return.attrName.return_tips", "returnTips", FieldTypeEnum.MEMO.getType(), 3, 0, 1, ShowTypeEnum.NONE.getCode(), 9890, 0, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    RETURN_AMOUNT(RefundBaseEnum.RETURN_AMOUNT.getAttr(), "enum.return.attrName.return_amount", "returnAmount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9890, 1, 1, ScreenTypeEnum.SCREEN_GROUP.getType()),
    REFUND_REASON(RefundBaseEnum.REFUND_REASON.getAttr(), "enum.return.attrName.refund_reason", "refundReason", FieldTypeEnum.COMBO.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9880, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    //退货单的订单状态 ReturnStatusEnum
    ORDER_STATUS("text_67","enum.return.attrName.order_status","orderStatus",FieldTypeEnum.COMBO.getType(),1,1,0,ShowTypeEnum.LIST_DETAIL.getCode(),9369,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    // 退货状态 RefundStatusEnum
    REFUND_STATUS(RefundBaseEnum.REFUND_STATUS.getAttr(),"enum.return.attrName.refund_status","returnStatus",FieldTypeEnum.COMBO.getType(),1,1,0,ShowTypeEnum.LIST_DETAIL.getCode(),9370,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    //退款状态 DrawbackStatusEnum
//    DRAWBACK_STATUS("text_69","enum.return.attrName.drawback_status","drawbackStatus",FieldTypeEnum.COMBO.getType(),1,1,0,ShowTypeEnum.LIST_DETAIL.getCode(),9370,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),
    // 退单类型 ReturnTypeEnum
    REFUND_TYPE("text_70","enum.return.attrName.refund_type","refundType",FieldTypeEnum.COMBO.getType(),1,1,0,ShowTypeEnum.LIST_DETAIL.getCode(),9370,1,0,ScreenTypeEnum.SCREEN_GROUP.getType()),

    MEMO(RefundBaseEnum.MEMO.getAttr(), "enum.return.attrName.memo", "memo", FieldTypeEnum.TEXTAREA.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9870, 1, 0, ScreenTypeEnum.SCREEN_SHOW.getType()),

    IMAGE("file_1","enum.return.attrName.image","image",FieldTypeEnum.IMAGE.getType(),1,0,0, ShowTypeEnum.ALL.getCode(),9871,1,0, ScreenTypeEnum.SCREEN_SHOW.getType()),
    // 退款信息
    REFUND_INFO(RefundBaseEnum.REFUND_INFO.getAttr(), "enum.return.attrName.refund_info", "refundInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9860, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    PAYMENT_SHEETS(RefundBaseEnum.PAYMENT_SHEETS.getAttr(), "enum.return.attrName.payment_sheets", "paymentSheets", FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), 1, 0, 0, ShowTypeEnum.EDIT.getCode(), 9850, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    REFUND_AMOUNT(RefundBaseEnum.REFUND_AMOUNT.getAttr(), "enum.return.attrName.refund_amount", "refundAmount", FieldTypeEnum.NUM.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9840, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    INVOICES(RefundBaseEnum.INVOICES.getAttr(), "enum.return.attrName.invoices", "invoices", FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), 1, 0, 0, ShowTypeEnum.EDIT.getCode(), 9830, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //text_81：红冲应收款
    PAYMENTS(RefundBaseEnum.PAYMENTS.getAttr(), "enum.return.attrName.payments", "payments", FieldTypeEnum.LINK_BUSINESS_MULTI.getType(), 1, 0, 0, ShowTypeEnum.EDIT.getCode(), 9825, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // 系统信息
    SYSTEM_INFO(RefundBaseEnum.SYSTEM_INFO.getAttr(), "enum.return.attrName.system_info", "systemInfo", FieldTypeEnum.SEPARATOR.getType(), SetTypeEnum.DISABLE_REPEAT.getCode(), 1, 0, ShowTypeEnum.EDIT_DETAIL.getCode(), 9820, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    //下单人
    ORDERER_ID("long_1","enum.return.attrName.orderer_id","ordererId",FieldTypeEnum.LONG.getType(),1, 0, 1, ShowTypeEnum.NONE.getCode(), 9670, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    ORDERER("text_83", "enum.return.attrName.orderer","orderer", FieldTypeEnum.TEXT.getType(), 1,0, 1, ShowTypeEnum.NONE.getCode(), 9670, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),

    CREATOR_ID(RefundBaseEnum.CREATOR_ID.getAttr(), "enum.return.attrName.creator_id", "creatorId", FieldTypeEnum.CREATORID.getType(), 1, 0, 0, ShowTypeEnum.ALL.getCode(), 9810, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    ADD_TIME(RefundBaseEnum.ADD_TIME.getAttr(), "enum.return.attrName.add_time", "addTime", FieldTypeEnum.ADDTIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9800, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    UPDATE_TIME(RefundBaseEnum.UPDATE_TIME.getAttr(),"enum.return.attrName.update_time", "updateTime", FieldTypeEnum.UPDATETIME.getType(), 1, 1, 0, ShowTypeEnum.ALL.getCode(), 9790, 1, 0, ScreenTypeEnum.SCREEN_GROUP.getType()),
    // 退货单模板设置界面不需要显示字段
    RED_CONTRACT_ID(RefundBaseEnum.RED_CONTRACT_ID.getAttr() , "enum.return.attrName.red_contract_id", "redContractId", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9780, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    LINK_DISTRIBUTOR_LINKED_TEXT(RefundBaseEnum.CUSTOMER_ID_LINKED_TEXT.getAttr() , "enum.return.attrName.distributor_user_id_linked_text", "customerIdLinkedText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9770, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    ORDER_ID_LINKED_TEXT(RefundBaseEnum.CONTRACT_ID_LINKED_TEXT.getAttr() , "enum.return.attrName.order_id_linked_text", "contractIdLinkedText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9760, 1, 1, ScreenTypeEnum.SCREEN_OFF.getType()),
    PAYMENT_SHEETS_LINKED_TEXT(RefundBaseEnum.PAYMENT_SHEETS_LINKED_TEXT.getAttr() , "enum.return.attrName.payment_sheets_linked_text", "paymentSheetsLinkedText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9750, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    INVOICES_LINKED_TEXT(RefundBaseEnum.INVOICES_LINKED_TEXT.getAttr() , "enum.return.attrName.invoices_linked_text", "invoicesLinkedText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9740, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // text_82：关联红冲应收隐藏
    PAYMENTS_LINKED_TEXT(RefundBaseEnum.PAYMENTS_LINKED_TEXT.getAttr() , "enum.return.attrName.payments_linked_text", "paymentsLinkedText", FieldTypeEnum.TEXT.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9735, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    INSTOCK_ID(RefundBaseEnum.INSTOCK_ID.getAttr() , "enum.return.attrName.instock_id", "instockId", FieldTypeEnum.COMBO_CHECK.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9730, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),
    // 0:部分(未)入库 1：已入库完毕 2：不需要入库
    ALL_IN_BOUND(RefundBaseEnum.ALL_IN_BOUND.getAttr() , "enum.return.attrName.all_in_bound", "allInBound", FieldTypeEnum.COMBO_NUM.getType(), 1, 0, 1, ShowTypeEnum.NONE.getCode(), 9720, 1, 0, ScreenTypeEnum.SCREEN_OFF.getType()),;


    /**
     * 英文名称
     */
    private String attr;
    /**
     * 中文名称
     */
    private String attrName;
    /**
     * 对应saas中的attr
     */
    private String saasAttr;
    /**
     * 字段类型，详见
     *
     * @see FieldTypeEnum
     */
    private Integer fieldType;
    /**
     * 重命名、判重、启用必填等权限
     * 1.重命名+字段权限
     * 2.重命名+字段权限+查重
     * 3.重命名+字段权限+启用、必填
     * 4.重命名+字段权限+查重+启用、必填
     * 注：pro中启用和必填是同一个权限，同时启用或同时关闭
     */
    private Integer setType;
    /**
     * 模板设计阶段字段是否允许编辑
     * 1：允许编辑
     * 0：不允许编辑
     */
    private Integer designEditable;
    /**
     * 字段是否显示在模板设计界面
     * 1：不显示
     * 0：显示
     */
    private Integer editHide;
    /**
     * 字段在各个界面（新建、编辑、列表、详情）的展现形式
     * 0：全部都显示
     * 1：新建编辑显示
     * 2：列表显示
     * 3：详情显示
     * 4：新建编辑、列表显示
     * 5：新建编辑、详情显示
     * 6：列表、详情显示
     * 7: 全部都不显示
     */
    private Integer showType;
    /**
     * 字段在模板设计界面的排序
     * 建议不要用连续的数字，可以用等差数列，以防以后在两个字段之间加入新字段
     */
    private Integer sort;
    /**
     * 字段在各个界面是否可见(启用)
     * 1：可见
     * 0：不可见
     * 注：字段在具体页面的可见性需要用属性和showType联合判断，在visible=1的情况下判断showType
     */
    private Integer visible;
    /**
     * 字段是否必填
     * 1：必填
     * 0：不必填
     */
    private Integer required;

    /**
     * 筛选类型
     * 1：可筛选
     * 2：不可筛选
     */
    private Integer screenType;

    ReturnEnum(String attr, String attrName, String saasAttr, Integer fieldType, Integer setType,
               Integer designEditable, Integer editHide, Integer showType, Integer sort, Integer visible, Integer required, Integer screenType) {
        this.attr = attr;
        this.attrName = attrName;
        this.saasAttr = saasAttr;
        this.fieldType = fieldType;
        this.setType = setType;
        this.designEditable = designEditable;
        this.editHide = editHide;
        this.showType = showType;
        this.sort = sort;
        this.visible = visible;
        this.required = required;
        this.screenType = screenType;
    }

    /**
     * 流程中获取隐藏字段
     *
     * @return
     */
    public static List<ReturnEnum> getDisableField() {
        List<ReturnEnum> returnEnums = new ArrayList<>();
        for (ReturnEnum returnEnum : values()) {
            if (Objects.equals(returnEnum.getShowType(), ShowTypeEnum.NONE.getCode()) || Objects.equals(returnEnum.getEditHide(), 1)
                    || Objects.equals(returnEnum.getFieldType(), FieldTypeEnum.CREATORID.getType()) || Objects.equals(returnEnum.getFieldType(), FieldTypeEnum.ADDTIME.getType())) {
                returnEnums.add(returnEnum);
            }
        }
        return returnEnums;
    }

    /**
     * 对data内的字段拼上“data.”
     *
     * @param cache
     * @return java.lang.String
     * @author zcp
     * @date 2019/3/1 18:12
     * @version v1.0
     * @since v1.0
     */
    public static String getAttrConnectData(ReturnEnum cache) {
        return "data." + cache.getAttr();
    }

    /**
     * 获取es查询、聚合字段
     * data在库内是json对象，对data字段内某个key做es【精确查询】时，需要在其前拼上“data.”，且【text等类型的】要在后面拼“.keyword”
     *
     * @param cache 当前枚举对象
     * @return java.lang.String 处理后的聚合字段
     * @author zcp
     * @date 2019/2/21 14:59
     * @version v1.0
     * @since v1.0
     */
    public static String getEsAttr4Keyword(ReturnEnum cache) {
        FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(cache.fieldType);
        //字段类型为text，或者字段在es内的类型时text，则需要在后面拼上“.keyword”
        boolean flag = fieldTypeEnum == FieldTypeEnum.TEXT || "text".equals(fieldTypeEnum.getEsalias());
        if (flag) {
            return "data." + cache.getAttr() + ".keyword";
        }
        return "data." + cache.getAttr();
    }

    /**
     * app端详情默认标题显示字段
     *
     * @return List
     * @author GuoJun.HU
     * @date 2019/4/8 19:21
     * @since V1.0
     */
    public static List<Object> getTitleAttrList() {
        return Collections.singletonList(RETURN_NO);
    }

    /**
     * 详情默认摘要
     *
     * @author long.rao
     * @date 2019-06-27 15:47
     */
    public static List<Object> getSummaryAttrList() {
        return Arrays.asList(ORDER_ID, SIGN_USER, LINK_DISTRIBUTOR);
    }

    /**
     * 把传入的字段（类型为LINK_BUSINESS_SINGLE）的转为其隐藏字段
     * @param attr 字段名
     * @return java.lang.String
     * @author zcp
     * @date 2019/7/4 15:31
     * @since v1.0
     * @version v1.0
     */
    public static String changeToHide4Link(String attr) {
        if (Objects.equals(attr, LINK_DISTRIBUTOR.attr)) {
            return LINK_DISTRIBUTOR_LINKED_TEXT.attr;
        } else if (Objects.equals(attr, LINK_DISTRIBUTOR.attr)) {
            return LINK_DISTRIBUTOR_LINKED_TEXT.attr;
        } else if (Objects.equals(attr, PAYMENT_SHEETS.attr)) {
            return PAYMENT_SHEETS_LINKED_TEXT.attr;
        } else if (Objects.equals(attr, PAYMENTS.attr)) {
            return PAYMENTS_LINKED_TEXT.attr;
        } else if (Objects.equals(attr, INVOICES.attr)) {
            return INVOICES_LINKED_TEXT.attr;
        }
        return attr;
    }

    public String getAttrName() {
        return I18nMessageUtil.getMessage(attrName);
    }

    public static String getI18nValue(String value) {
        return I18nMessageUtil.getMessage(value);
    }
}
